package net.hyperz.hackit;

import net.hyperz.hackit.fragments.FileSystem;
import net.hyperz.hackit.fragments.ProcessMgr;
import net.hyperz.hackit.util.SimpleGestureFilter;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;

public class Main extends Activity implements SimpleGestureFilter.SimpleGestureListener{
	private SimpleGestureFilter detector; 
	private ActionBar mActionBar;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		detector = new SimpleGestureFilter(this,this);

		mActionBar = this.getActionBar();
		mActionBar.addTab(mActionBar.newTab().setText("Process Manager").setTabListener(new tabsListener(new ProcessMgr())));
		mActionBar.addTab(mActionBar.newTab().setText("File System").setTabListener(new tabsListener(new FileSystem())));
		//actionBar.addTab(actionBar.newTab().setText("Terminal")); //.setTabListener(this));
		//actionBar.addTab(actionBar.newTab().setText("Event Logs")); //.setTabListener(this));
		//actionBar.addTab(actionBar.newTab().setText("RSS Feeds")); //.setTabListener(this));
		
		//ensure that home sends user back to main screen
		mActionBar.setDisplayHomeAsUpEnabled(true);
		//and remove the title no title for home button
		mActionBar.setDisplayShowTitleEnabled(false);
		//set that navigation mode is tabs
		mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
	}
	
	private void selectNextTab() {
		int selectedPosition = mActionBar.getSelectedTab().getPosition();
		if (selectedPosition != Tab.INVALID_POSITION) {
			if (selectedPosition == mActionBar.getTabCount() - 1) {
				//reset to first position
				mActionBar.setSelectedNavigationItem(0);
			} else {
				mActionBar.setSelectedNavigationItem(selectedPosition++);
			}
		}
	}

	private void selectPrevTab() {
		int selectedPosition = mActionBar.getSelectedTab().getPosition();
		if (selectedPosition != Tab.INVALID_POSITION) {
			if (selectedPosition == 0) {
				//reset to last position
				mActionBar.setSelectedNavigationItem(mActionBar.getTabCount() - 1);
			} else {
				mActionBar.setSelectedNavigationItem(selectedPosition--);
			}
		}
	}

	@Override 
	public boolean dispatchTouchEvent(MotionEvent me){ 
		this.detector.onTouchEvent(me);
		return super.dispatchTouchEvent(me); 
	}

	@Override
	public void onSwipe(int direction) {
		switch (direction) {
		case SimpleGestureFilter.SWIPE_RIGHT:
			selectNextTab();
			break;
		case SimpleGestureFilter.SWIPE_LEFT:
			selectPrevTab();
			break;
		default:
			break;
		} 

	}

	@Override
	public void onDoubleTap() {
		Toast.makeText(this, "Double Tap", Toast.LENGTH_SHORT).show(); 
	}

	class tabsListener implements ActionBar.TabListener {
		public Fragment fragment;

		public tabsListener(Fragment fragment) {
			this.fragment = fragment;
		}

		@Override
		public void onTabReselected(Tab tab, FragmentTransaction ft) {
			//Toast.makeText(StartActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
		}

		@Override
		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			ft.replace(R.id.fragment_container, fragment);
		}

		@Override
		public void onTabUnselected(Tab tab, FragmentTransaction ft) {
			ft.remove(fragment);
		}
	}
}


